package com.sdy.controller;

import com.jfinal.core.Controller;
import com.sdy.model.Position;
import com.sdy.util.AjaxDone;

public class PositionController extends Controller {
	
	private static Position dao = Position.dao;
	
	public void datagrid() {
		AjaxDone j = new AjaxDone();
		try {
			renderJson(dao.datagrid(getParaToInt("page"),getParaToInt("rows")));
		} catch (Exception e) {
			e.printStackTrace();
			j.setErrorMsg(e.getMessage());
			renderJson(j);
		}
	}
	
	public void getComboboxValue() {
		renderJson(dao.find("select * from position"));
	}
	
	/**
	 * 添加部门
	 */
	public void doAdd() {
		AjaxDone j = new AjaxDone();
		try {
			if(!getModel(Position.class).save()) {
				throw new RuntimeException("保存失败!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			j.setErrorMsg(e.getMessage());
		}
		renderJson(j);
	}
	
	/**
	 * 删除部门
	 */
	public void doDelete() {
		AjaxDone j = new AjaxDone();
		try {
			if(!dao.deleteById(getParaToInt("id")))
				throw new RuntimeException("删除异常!");
		} catch (Exception e) {
			e.printStackTrace();
			j.setErrorMsg(e.getMessage());
		}
		renderJson(j);
	}
	
	/**
	 * 加载修改数据
	 */
	public void goUpdate() {
		AjaxDone j = new AjaxDone();
		try {
			renderJson(dao.getById(getParaToInt("id")));
		} catch (Exception e) {
			j.setErrorMsg(e.getMessage());
			e.printStackTrace();
			renderJson(j);
		}
	}
	
	public void doUpdate() {
		AjaxDone j = new AjaxDone();
		try {
			if(!getModel(Position.class).update())
				throw new RuntimeException("保存失败!");
		} catch (Exception e) {
			j.setErrorMsg(e.getMessage());
			e.printStackTrace();
		}
		renderJson(j);
	}
}
